package cn.cherish.type.batch;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;

import java.util.ArrayList;
import java.util.List;

public class Producer {
    public static void main(String[] args) throws Exception {
        /**
         1. 谁来发？
         2. 发给谁？
         3. 怎么发？
         4. 发什么？
         5. 发的结果是什么？
         6. 打扫战场
         **/

        //1.创建一个发送消息的对象Producer
        DefaultMQProducer producer = new DefaultMQProducer("group1");
        //2.设定发送的命名服务器地址
        producer.setNamesrvAddr("localhost:9876");
        //3.1启动发送的服务
        producer.setSendMsgTimeout(1000000);
        producer.start();
        System.out.println("生产者启动！");
        //4.创建要发送的消息对象,指定topic，指定内容body
        List<Message> messageList = new ArrayList<>();
        String message = "hello MQ_1";
        Message msg1 = new Message("topic7","tag1",message.getBytes("UTF-8"));
        Message msg2 = new Message("topic7","tag1",message.getBytes("UTF-8"));
        Message msg3 = new Message("topic7","tag1",message.getBytes("UTF-8"));
        messageList.add(msg1); messageList.add(msg2); messageList.add(msg3);
        //3.2发送消息
        producer.send(messageList);
        //5.关闭连接
        producer.shutdown();
    }
}
